<?php
declare (strict_types = 1);

namespace app\controller;

use app\common\BaseController;
use app\common\EncryptKeys;
use think\facade\Validate;
use think\Request;
use app\service\UserService;

class UserController extends BaseController
{
  /**
   * 显示资源
   *
   * @return \think\Response
   */
  public function index(Request $request){
    $groupid = $request->param('groupid', []);
    $conditions = [];
    if ($groupid) {
      $conditions[] = ['groupid', $groupid, 'IN'];
    }
    $order = ['id' => 'ASC'];
    $data = UserService::getInstance()->getUserByCondition($conditions, $order);
    foreach ($data as &$value) {
      $store = [];
      $value['operate_store'] = explode(',', $value['operate_store']);
      foreach ($value['operate_store'] as $v) {
        if ($v) {
          $store[] = intval($v);
        }
      }
      $value['operate_store'] = $store;
    }
    return $this->send200Response($data);
  }

  /**
   * 保存新建的资源
   *
   * @param  \think\Request  $request
   * @return \think\Response
   */
  public function save(Request $request){
    $data = $request->param('data',[]);
    if (!$data) {
      return $this->send400Response('data不能为空');
    }
    $result = UserService::getInstance()->updateMultiUser($data);
    if ($result['status']) {
      return $this->send200Response('ok');
    } else {
      return $this->send400Response($result['msg']);
    }
  }

  /**
   * 显示指定的资源
   *
   * @param  int  $id
   * @return \think\Response
   */
  public function read($id)
  {
    $user = UserService::getModel()
      ->find($id)
      ->toArray();
    return $this->send200Response($user);
  }

  /*
   * 登录
   */
  public function login(Request $request) {
    $data = $request->param();
    $validate = Validate::rule([
      'username' => 'require',
      'password' => 'require',
    ]);
    if (!$validate->check($data)) {
      return $this->send400Response($validate->getError());
    }
    $user = UserService::getInstance()->getUserByName($data['username']);
    if (!$user){
      return $this->send400Response('用户不存在');
    } elseif ($user['password'] != $data['password']) {
      return $this->send400Response('用户密码有误');
    }
    // 获取token
    $access_token = EncryptKeys::autoSSOEncryptKen();
    // 获取目录
    // 原料模块
    $material = [
      [
        'path' => 'material',
        'name' => '原料进销存',
        'children' => [
          [
            'name' => '原料库存情况',
            'path' => 'material-list'
          ],
          [
            'name' => '原料采购明细',
            'path' => 'material-in'
          ]
        ]
      ]
    ];
    // 肉码模块
    $product = [
      [
        'path' => 'product',
        'name' => '肉码进销存',
        'children' => [
          [
            'name' => '肉码库存情况',
            'path' => 'product-list'
          ],
          [
            'name' => '肉码生产记录',
            'path' => 'product-in'
          ]
        ]
      ]
    ];
    // 肉码生产成本计算
    $process = [
      [
        'path' => 'process-list',
        'name' => '肉码生产成本'
      ]
    ];
    // 出入库明细
    $inout = [
      [
        'path' => 'inout-list',
        'name' => '出入库明细'
      ]
    ];
    // 申请审批模块
    $apply = [
      [
        'path' => 'apply',
        'name' => '申请审批',
        'children' => [
          [
            'name' => '审批列表',
            'path' => 'apply-list'
          ],
          [
            'name' => '已审批列表',
            'path' => 'applied-list'
          ]
        ]
      ]
    ];
    // 商品模块
    $goods = [
      [
        'path' => 'goods-list',
        'name' => '商品列表'
      ]
    ];
    // 外部采购
    $outPurchase = [
      [
        'path' => 'outer-purchase-list',
        'name' => '外部采购'
      ]
    ];
    // 仓库现存
    $store = [
      [
        'path' => 'store-house-list',
        'name' => '仓库现存'
      ]
    ];
    // 退货单
    $storeBack = [
      [
        'path' => 'store-back-list',
        'name' => '仓库-退货单'
      ]
    ];
    // 内部采购
    $inPurchase = [
      [
        'path' => 'inner-purchase-list',
        'name' => '内部采购'
      ]
    ];
    // 损耗记录
    $loss = [
      [
        'path' => 'store-loss-list',
        'name' => '损耗记录/填库存'
      ]
    ];
    // 调拨物资
    $allocation = [
      [
        'path' => 'material-allocation-list',
        'name' => '调拨物资'
      ]
    ];
    // 进销存表格
    $download = [
      [
        'path' => 'export',
        'name' => '下载表格'
      ]
    ];
    // 高级设置
    $setting = [
      [
        'path' => 'advanced-setting',
        'name' => '高级设置',
        'children' => [
          [
            'name' => '运营账号管理',
            'path' => 'operate-store'
          ]
        ]
      ]
    ];

    $access = [];
    $base = 0;
    if ($user['groupid'] == 1) {
      $access = array_merge($material, $product, $process, $inout, $apply, $download, $storeBack, $store, $inPurchase, $outPurchase, $loss);
    } elseif ($user['groupid'] == 0) {
      $material = [
        [
          'path' => 'material',
          'name' => '原料进销存',
          'children' => [
            [
              'name' => '原料库存情况',
              'path' => 'material-list'
            ],
            [
              'name' => '原料采购明细',
              'path' => 'material-in'
            ],
            [
              'name' => '原料采购单',
              'path' => 'material-purchase'
            ]
          ]
        ]
      ];
      $access = array_merge($material, $product, $process, $inout, $setting, $goods, $download, $outPurchase);
    } elseif ($user['groupid'] == 2) {
      $access = array_merge($material);
    } elseif ($user['groupid'] == 3) {
      $access = array_merge($apply, $goods, $store, $inPurchase, $outPurchase, $loss);
    } elseif ($user['groupid'] == 4) {
      $access = array_merge($apply, $setting, $goods, $store, $inPurchase, $outPurchase, $loss);
    } elseif ($user['groupid'] == 8) {
      $access = array_merge($store, $inPurchase, $outPurchase, $loss, $allocation);
      $base = $user['base'];
    } elseif ($user['groupid'] == 9) {
      $setting = [
        [
          'path' => 'advanced-setting',
          'name' => '高级设置',
          'children' => [
            [
              'name' => '肉码出入库回滚',
              'path' => 'process-back'
            ]
          ]
        ]
      ];
      $access = array_merge($setting);
    }
    $data = [
      'access_token' => $access_token,
      'groupid' => $user['groupid'],
      'userid' => $user['id'],
      'access' => $access,
      'base' => $base
    ];
    return $this->send200Response($data);
  }

  /*
   * 修改密码
   */
  public function loginPasswd(Request $request){
    $data = $request->param();
    $validate = Validate::rule([
      'passwd' => 'require',
      'newPasswd' => 'require',
      'passwdCheck' => 'require',
      'username' => 'require'
    ]);
    if (!$validate->check($data)) {
      return $this->send400Response($validate->getError());
    }
    if ($data['newPasswd'] !== $data['passwdCheck']) {
      return $this->send400Response('确认密码不一致');
    }
    // 获取用户
    $user = UserService::getInstance()->getUserByName($data['username']);
    if ($user['password'] !== $data['passwd']) {
      return $this->send400Response('密码有误');
    }
    $update = [
      'password' => $data['newPasswd']
    ];
    $res = UserService::getInstance()->updateUser($user['id'], $update);
    return $this->send200Response(boolval($res));
  }

  public function StoreAll(){
    $data = UserService::getInstance()->storeAll();
    return $this->send200Response($data);
  }
}

